package com.TSum;

import java.util.HashMap;

public class TSum {

    // 1. 暴力枚举
    public static int[] violenceEnumeration(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (target == nums[i] + nums[j]) {
                    return new int[]{i, j};
                }
            }
        }
        throw new IllegalArgumentException("No two sun solution!");
    }


    // 2.哈希表
    public static int[] hashTable(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            } else map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution!");
    }
}
